"use strict";

var config = getSysConfig("cron");
var status = false;
var timer;
var common = requireMod("common");
var request = common.request;
var logger = log.getLogger("transport_log");
var fs = require("fs");
var path = require("path");

const logPath = path.resolve(__dirname,"../private/log/");

const nginx_pv_tpl = '{ip} - - [{date}] "GET /visit.gif?{data} HTTP/1.1" 200 35 "{referer}" "{ua}" "-"';
const nginx_click_tpl = '{ip} - - [{date}] "GET /button.gif?{data} HTTP/1.1" 200 35 "{referer}" "{ua}" "-"';


function fetchData() {

    let format = config.transportLog.format;

    let types = ["pageview","click","event"];

    let time = common.date("YmdH", Date.now());

    fs.readdir(logPath, function(err, fileNames){
        fileNames.forEach(function(fileName){
            let filePath = fileName.indexOf(time) !== -1 ? logPath +"/" + fileName : "";
            if (filePath) {
                fs.readFile(filePath, "utf8",function(err, file){
                    if(err) {
                        logger.error(err);
                        return;
                    }
                    let logs = file.split("\n");
                    let formatedLog = [];
                    logs.forEach(function(log){
                        if (log) {
                            console.log(log);
                        }
                    });
                });
            }
        });
    });
    
}

module.exports = fetchData;

/**
 * 获取延迟时间
 * @return {Int} 延迟时间
 */
module.exports.getDelay = function() {
    return (config.transportLog.time - config.danger_time) * 1000;
};

/**
 * 获取任务开启状态
 * @return {Boolean} 
 */
module.exports.enable = function() {
    return config.transportLog.enable;
};